package gameAppLayer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;

import stars.enums.StarParameters;
import stars.enums.StarTypeData;

import gameAppLayer.enums.Constants;
import audio.enums.AudioInformation;
import audio.enums.AudioParameters;

import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioData;
import com.jme3.audio.AudioKey;
import com.jme3.audio.AudioNode;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Node;
import com.jme3.texture.Texture;


/**
 * Loads all game assets using JME AssetManager.  Once loaded they are in the AssetManager cache and can be
 * quickly referenced for future uses.
 * 
 * @author RJ
 *
 */
public class AssetPreLoader {
	
	private AssetManager assetManager;
	private ExternalData externalData;
//	private AssetMap assetMap;
	
	public AssetPreLoader(
			AssetManager assetManager,
			ExternalData externalData) {
		this.assetManager = assetManager;
		this.externalData = externalData;
//		this.assetMap = new AssetMap(assetManager);
	}
	
//	public AssetMap getAssetMap() {
//		return this.assetMap;
//	}
	
	public boolean init(HashMap<String, String> files) {
		
		//QQQ:  Automate this section to search through entire directories, load all files with relevant extensions, use AssetFileFinder in utilities package
		
		//Build star materials from externalData
		StarParameters starParams = externalData.getStarParameters();
		ArrayList<String> starList = starParams.getEntries();
		StarTypeData starData;
		for (String starType: starList) {
			starData = starParams.getParameter(starType);
			
			ArrayList<Float> colorRGB = starData.getRGBColorVals();
			ArrayList<Float> colorGlow = starData.getRGBGlowColorVals();
			
			Texture quiet = AssetPortal.loadTexture(starData.getTexture("Quiet"));
			Texture average = AssetPortal.loadTexture(starData.getTexture("Average"));
			Texture active = AssetPortal.loadTexture(starData.getTexture("Active"));
			Texture blank = AssetPortal.loadTexture(starData.getTexture("Blank"));
			Texture far = AssetPortal.loadTexture(starData.getTexture("Far"));
			
			Material quietMaterial = new Material(assetManager, Constants.unshadedMaterial);
			quietMaterial.setTexture("ColorMap", quiet);
			quietMaterial.setColor("Color", new ColorRGBA(colorRGB.get(0), colorRGB.get(1), colorRGB.get(2), 0.1f));
			quietMaterial.setColor("GlowColor", new ColorRGBA(colorGlow.get(0), colorGlow.get(1), colorGlow.get(2), 0.1f));
			
			Material averageMaterial = new Material(assetManager, Constants.unshadedMaterial);
			averageMaterial.setTexture("ColorMap", average);
			averageMaterial.setColor("Color", new ColorRGBA(colorRGB.get(0), colorRGB.get(1), colorRGB.get(2), 0.1f));
			averageMaterial.setColor("GlowColor", new ColorRGBA(colorGlow.get(0), colorGlow.get(1), colorGlow.get(2), 0.1f));
			
			Material activeMaterial = new Material(assetManager, Constants.unshadedMaterial);
			activeMaterial.setTexture("ColorMap", active);
			activeMaterial.setColor("Color", new ColorRGBA(colorRGB.get(0), colorRGB.get(1), colorRGB.get(2), 0.1f));
			activeMaterial.setColor("GlowColor", new ColorRGBA(colorGlow.get(0), colorGlow.get(1), colorGlow.get(2), 0.1f));
			
			Material blankMaterial = new Material(assetManager, Constants.unshadedMaterial);
			blankMaterial.setTexture("ColorMap", blank);
			blankMaterial.setColor("Color", ColorRGBA.White);
//			blankMaterial.setColor("GlowColor", ColorRGBA.White);
			blankMaterial.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
			
			Material farMaterial = new Material(assetManager, Constants.unshadedMaterial);
			farMaterial.setTexture("ColorMap", far);
			farMaterial.setColor("Color", new ColorRGBA(colorRGB.get(0), colorRGB.get(1), colorRGB.get(2), 1.0f));
//			farMaterial.setColor("GlowColor", new ColorRGBA(colorGlow.get(0), colorGlow.get(1), colorGlow.get(2), 1.0f));
			farMaterial.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
			
			AssetPortal.addMaterial(starType + "Quiet", quietMaterial);
			AssetPortal.addMaterial(starType + "Average", averageMaterial);
			AssetPortal.addMaterial(starType + "Active", activeMaterial);
			AssetPortal.addMaterial(starType + "Blank", blankMaterial);
			AssetPortal.addMaterial(starType + "Far", farMaterial);
		}
		
		//Build AudioNodes from externalData
		AudioParameters audioParams = externalData.getAudioParameters();
		ArrayList<String> audioList = audioParams.getEntries();
		AudioInformation data;
		AudioKey key;
		boolean streaming;
		AudioNode audioNode;
		AudioData audioData;
		
		for (String sound: audioList) {
			data = audioParams.getParameter(sound);			
			streaming = data.getStreaming();
			String filePath = data.getFileName();
//			String filePath = Constants.audioPath+data.getFileName();
			String audioEventName = data.getAudioEventName();
			
			key = new AudioKey(filePath, streaming);
			audioData = AssetPortal.getAudioData(filePath);
			audioNode = new AudioNode(audioData, key);
			
			AssetPortal.addAudioNode(audioEventName, audioNode);
		}
		
		
		
		
		

		
		
		return true;
	}
}